class ListController < ApplicationController

  def index
    @lists = List.find_all_lists
  end
  
  
  
  def create_new_list
    @new_list = List.new(params[:create]) #:create = form name
    @new_list.save
    if @new_list.save
      redirect_to_items(@new_list.id, "You have successfully created the list! #{@new_list.save}")
    else
      redirect_to_index("not successful.  no list created #{@new_list.save}")
    end 
  end
  
  def show
    @list = List.info(params[:id])
    #@list_items = Item.all_items_by_list_id(params[:id]) 
    @list_items = Item.find(:all, :conditions => ["status = 't' AND list_id = '#{params[:id]}'"])
    @inactive_items = Item.find(:all, :conditions => ["status = 'f' AND list_id = '#{params[:id]}'"])
  end
  
  def edit
    @list = List.info(params[:id])
    # @items = Item.all_items_by_list_id(params[:id])
    @items = Item.find(:all, :conditions => ["list_id = #{params[:id]} and status = 't'"]) 
  end
  
  def reorder
    @list = List.info(params[:id])
    @items = Item.all_items_by_list_id(params[:id])
  end
  
  def delete
    @list = List.info(params[:id])
    List.find(params[:id]).destroy
    Item.delete_all "list_id = #{params[:id]}"
    redirect_to_index(@list.name + " has been deleted.")
    
  end
  
  def add_list_item
    @new_item = Item.new(params[:add_items])
    if @new_item.save
      redirect_to(:action => :show, :id => @new_item.list_id) #params[:list_id]
      #redirect_to_items(params[:list_id], "You have successfully added the list item ") #'#{@new_item.content}'
    else
      redirect_to_index("not successful.  no list item created #{@new_item.id}")
    end
  end
  
  def item_edit    
    Item.find(params[:item_id]).destroy
    redirect_to(:action => :edit, :id => params[:list_id])
  end
  
  def item_status
    @item = Item.find(params[:item_id])
    @item.status = params[:item_status]
    @item.save
    redirect_to(:action => :show, :id => params[:list_id])
  end
  
  protected
  def redirect_to_index(msg)
    flash[:notice] = msg
    redirect_to :action => :index
  end
  
  def redirect_to_items(list_id, msg)
    @list_id = list_id
    flash[:notice] = msg
    redirect_to(:action => :show, :id => list_id)
  end
  
  
  
end
